package br.uefs.dexa.rfframework.gui.utils.paging;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.uefs.dexa.rfframework.config.AppConfigManager;
import br.uefs.dexa.rfframework.gui.utils.WrapLayout;
import br.unicamp.ic.lis.rfframework.objects.DigitalObject;
import br.unicamp.ic.lis.rfframework.objects.DigitalObjectSet;

public class PagingControlPanel extends JPanel {
	
	
	private static final long serialVersionUID = -2648219288230330697L;

	public static final int HORIZONTAL_GAP = 20;
	
	public static final int TOTAL_LABELS_TO_SHOW = 10;
	
	
	
	private ArrayList<PagingLabel> labels = new ArrayList<PagingLabel>();
	

	private PagingOutputObjectSetContainerPanel pagingPanel;
	
	
	protected PagingControlPanel(PagingOutputObjectSetContainerPanel pagingPanel)
	{
		this.pagingPanel = pagingPanel;
		WrapLayout layout = new WrapLayout();
		layout.setHgap(HORIZONTAL_GAP);
		setLayout(layout);
		setPreferredSize(new Dimension(1000, 50));
		setVisible(false);
	}
	
	public void generateLabels(DigitalObjectSet<DigitalObject> objects)
	{
		int noLabels =  ( objects.getSize() / AppConfigManager.getInstance().getMaxItensPerPage() ) + 1;
		
		for (int i = 1; i <= noLabels ; i++) {
			
			PagingLabel label = new PagingLabel(i, pagingPanel);
			label.setVisible(false);

			labels.add(label);
		}
		
		resetLabels();
	}
	
	protected void addLabels()
	{
		removeAll();
		
		add(new PreviousLabel(pagingPanel));
		
		for (JLabel jLabel : labels) {
			add(jLabel);
		}
		
		add(new NextLabel(pagingPanel));
	}
	
	protected void markCurrentPageLabel(int page)
	{
		PagingLabel label = labels.get(page - 1);
		label.markAsCurrentPage();
	}
	
	
	protected void resetLabels()
	{
		for (PagingLabel label : labels) {
			label.reset();
		}
	}
	
	protected void disableLabels()
	{
		for (PagingLabel label : labels) {
			label.removeMouseListeners();
		}
	}
	
	protected void enableLabels()
	{
		for (int i = 0; i <labels.size(); i++) {
			labels.get(i).addMouseListener();
		}
	}
	
	protected void showOnlyNeighborLabels(int reference)
	{
		
		if (labels.get(reference-1) ==  null)
			throw new NullPointerException("invalid page number!");
		
		
		int max = labels.size();
		int half = (int) Math.ceil( (double) TOTAL_LABELS_TO_SHOW / 2.0);
		int left = reference - 1;
		int right = max - reference;
		int counter = 0;
		
		labels.get(reference-1).setVisible(true);
		
		if(left >= half && right >= half-1) 
		{
			//left side
			for (int i = left; i > 0; i--) {
				
				if(reference-i <= half && counter<=TOTAL_LABELS_TO_SHOW ) {
					labels.get(i-1).setVisible(true);
					counter++;
				}
				else
					labels.get(i-1).setVisible(false);
			}
			
			//right side
			for (int i = reference+1; i < max+1; i++) {
				
				if(i-reference <= half-1 && counter<=TOTAL_LABELS_TO_SHOW) {
					labels.get(i-1).setVisible(true);
					counter++;
				}
				else
					labels.get(i-1).setVisible(false);
				
			}
		}
		
		
		if(left < half)
		{
			int remainder = TOTAL_LABELS_TO_SHOW - left - 1;
			
			//left side
			for (int i = left; i > 0; i--) {
				labels.get(i-1).setVisible(true);
				counter++;
			}
			
			//right side
			for (int i = reference+1; i < max+1; i++) {
				
				if(i-reference <= remainder && counter<=TOTAL_LABELS_TO_SHOW) {
					labels.get(i-1).setVisible(true);
					counter++;
				}
				else
					labels.get(i-1).setVisible(false);
				
			}
			
		}
		
		
		if(right < half-1)
		{
			int remainder = TOTAL_LABELS_TO_SHOW - right - 1;
			
			//right side
			for (int i = reference+1; i < max+1; i++) {
				labels.get(i-1).setVisible(true);
				counter++;
			}
			
			//left side
			for (int i = left; i > 0; i--) {
				
				if(reference-i <= remainder && counter<=TOTAL_LABELS_TO_SHOW) {
					labels.get(i-1).setVisible(true);
					counter++;
				}
				else
					labels.get(i-1).setVisible(false);
				
			}
			
			
			
		}
		
	}
	
	
	
}
